﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

// Directive for the data model.
using MobilnyDostawca.Model;


namespace MobilnyDostawca.ViewModel
{
    public class MobilnyDostawcaViewModel : INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private MobilnyDostawcaDataContext mdDB;

        // Class constructor, create the data context object.
        public MobilnyDostawcaViewModel(string mdDBConnectionString)
        {
            mdDB = new MobilnyDostawcaDataContext(mdDBConnectionString);
        }


        private ObservableCollection<Przesylka> _allPackages;
        public ObservableCollection<Przesylka> AllPackages
        {
            get { return _allPackages; }
            set
            {
                _allPackages = value;
                NotifyPropertyChanged("AllPackages");
            }
        }

        private ObservableCollection<Przesylka> _freePackages;
        public ObservableCollection<Przesylka> FreePackages
        {
            get { return _freePackages; }
            set
            {
                _freePackages = value;
                NotifyPropertyChanged("FreePackages");
            }
        }

        private ObservableCollection<Przesylka> _newPackages;
        public ObservableCollection<Przesylka> NewPackages
        {
            get { return _newPackages; }
            set
            {
                _newPackages = value;
                NotifyPropertyChanged("NewPackages");
            }
        }

        private ObservableCollection<Przesylka> _deliveringPackages;
        public ObservableCollection<Przesylka> DeliveringPackages
        {
            get { return _deliveringPackages; }
            set
            {
                _deliveringPackages = value;
                NotifyPropertyChanged("DeliveringPackages");
            }
        }

        private ObservableCollection<Przesylka> _deliveredPackages;
        public ObservableCollection<Przesylka> DeliveredPackages
        {
            get { return _deliveredPackages; }
            set
            {
                _deliveredPackages = value;
                NotifyPropertyChanged("DeliveredPackages");
            }
        }
       
        private List<Status> _statusList;
        public List<Status> StatusList
        {
            get { return _statusList; }
            set
            {
                _statusList = value;
                NotifyPropertyChanged("CategoriesList");
            }
        }

        // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {

            var packagesInDB = from Przesylka package in mdDB.Przesylki
                                select package;

            AllPackages = new ObservableCollection<Przesylka>(packagesInDB);
            FreePackages = new ObservableCollection<Przesylka>(packagesInDB.Where(r => r.Status.Nazwa == "wolna"));
            NewPackages = new ObservableCollection<Przesylka>(packagesInDB.Where(r => r.Status.Nazwa == "nowa"));
            DeliveringPackages = new ObservableCollection<Przesylka>(packagesInDB.Where(r => r.Status.Nazwa == "dostarczana_magazyn" || r.Status.Nazwa == "dostarczana_klient"));
            DeliveredPackages = new ObservableCollection<Przesylka>(packagesInDB.Where(r => r.Status.Nazwa == "doreczona"));

            var statusesInDB = from Status status in mdDB.Statusy
                                     select status;


            //foreach (Status status in statusesInDB)
            //{
            //    switch (status.Nazwa)
            //    {
            //        case "wolna":
            //            FreePackages = new ObservableCollection<Przesylka>(status.Przesylki);
            //            break;
            //        default:
            //            break;
            //    }
            //}

            StatusList = mdDB.Statusy.ToList();

        }



        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            mdDB.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
